//useBrandsQueryStore.ts
import { create } from "zustand";
import { useQuery } from "react-query";
import { useMemo } from "react";
import { BrandsQueryStore } from "./type";

export const brandsBaseUrl = "http://localhost:4000/clothing/brands";

export const useBrandsQueryStore = create<BrandsQueryStore>(() => ({
  brands: [],
}));

export function useBrandsAdminProducts() {
  const { data, isLoading, isError, isFetching } = useQuery(
    "brands",
    async () => {
      const response = await fetch(brandsBaseUrl);
      if (!response.ok) {
        throw new Error("Network response was not ok");
      }
      const data = await response.json();
      return data;
    }
  );

  const brandsAdminProducts = useMemo(() => data || [], [data]);

  return { brandsAdminProducts, isLoading, isError, isFetching };
}
